﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BackpackUI : MonoBehaviour
{
    private GameObject parentUI;
    public List<SlotUI> slotuiList;

    private void Awake()
    {
        parentUI = transform.Find("ParentUI").gameObject;
    }
    void Start()
    {
        InitUI();
    }
    // Update is called once per frame
    void Update()
    {
        //按键显示隐藏
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            ToggleUI();
        }
    }

    //初始化得到Slot编号对应仓库位置
    public void InitUI()
    {
        //创建一个长度为24的集合
        slotuiList = new List<SlotUI>(new SlotUI[24]);
        SlotUI[] slotuiArray = transform.GetComponentsInChildren<SlotUI>();
        //更新集合
        foreach (SlotUI slotUI in slotuiArray)
        {
            slotuiList[slotUI.index] = slotUI;
        }
        //更新UI的显示
        UpdataUI();
    }

    //更新UI
    public void UpdataUI()
    {
        //数据集合
        List<SlotData> slotdataList = InventoryManager.Instance.backpack.slotList;
        for(int i=0;i<slotdataList.Count;i++)
        {
            slotuiList[i].SetData(slotdataList[i]);
        }
    }
    //控制显示和隐藏
    public void ToggleUI()
    {
        parentUI.SetActive(!parentUI.activeSelf);
    }
    public void OnCloseClick()
    {
        ToggleUI();
    }
}
